import Notify from '@vant/weapp/notify/notify';
import { request } from '../../assets/js/request.js';
var value = '';
Page({
  formSubmit:function(e){
    wx.showModal({
      title: '提示',
      content: '您确定要修改信息吗？',
      success:(res) => {
        if(res.confirm){
          wx.showLoading({
            title: '正在保存',
            mask: true,
          })
          let reqData1 = {
            status: this.data.status,
            uid:this.data.uid,
            uname: e.detail.value.uname,
            usex: this.data.usex,
            uclass: e.detail.value.uclass,
            uemail: e.detail.value.uemail
          }
          request('/updataInfo.php', reqData1)
            .then((res)=>{
              if (res.data == 'ok') {
                Notify({ type: 'success', message: '修改成功' });
                this._getUserInfo();
              }else if(res.data == 'fail'){
                Notify({ type: 'danger', message: '修改失败，请检查班级编号是否填写正确' });
              }
            })
            .catch((err)=>{
              Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
            })
        }
      }
    })
  },
  onChange(event) {
    this.setData({
      usex: event.detail
    });
  },
  _getUserInfo: function () {
    wx.showLoading({
      title: '加载数据',
      mask: true,
    })
    let reqData2 = {
      uid: this.data.uid,
      status:this.data.status
    }
    request('/getUserInfo.php', reqData2)
      .then((res)=>{
        if (this.data.status == 'student') {
          this.setData({
            uname: res.data.stu_name,
            ufaculty: res.data.stu_faculty,
            umajor: res.data.stu_major,
            ugrade: res.data.stu_grade,
            uclass: res.data.stu_class,
            usex: res.data.stu_sex,
            uclassName: res.data.stu_className
          })
        }else{
          this.setData({
            uname: res.data.t_name,
            usex: res.data.t_sex,
            uemail: res.data.t_email,
          })
        }
      })
      .catch((err)=>{
        Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
      })
  },
  
  data: {
    status:'',
    uid:'',
    uname:'',
    ufaculty:'',
    umajor:'',
    ugrade:'',
    uclass:'',
    uclassName:'',
    usex: '',
    uemail:''
  },

  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      status: wx.getStorageSync('status'),
    })
    this._getUserInfo();
  },

})